﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace AMS.UI.Controls
{
    /// <summary>
    /// 基本组件的基类
    /// </summary>
    public partial class BaseComponent : UserControl
    {
        public BaseComponent()
        {
            InitializeComponent();
        }

        private BalloonControl balloonCtrl;

        /// <summary>
        /// 提示控件
        /// </summary>
        public BalloonControl Balloon
        {
            get
            {
                if (balloonCtrl == null)
                {
                    balloonCtrl = new BalloonControl(ParentForm);
                }
                return balloonCtrl;
            }
        }

        /// <summary>
        /// 启用延迟加载，使用默认的延迟时间(100ms)
        /// </summary>
        protected void ApplyDelayLoadData()
        {
            ApplyDelayLoadData(100);
        }

        /// <summary>
        /// 延时加载数据
        /// </summary>
        /// <param name="delayMiliseconds"></param>
        protected void ApplyDelayLoadData(int delayMiliseconds)
        {
            new App.DelayLoading(this, delayMiliseconds, new MethodInvoker(DelayLoadData)).Execute();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        protected virtual void DelayLoadData()
        {
        }

        private List<Control> readOnlyControls;
        /// <summary>
        /// 初始化时就是只读属性的控件。
        /// 需要使用者自己维护
        /// </summary>
        public List<Control> ReadOnlyControls
        {
            get
            {
                if (readOnlyControls == null)
                {
                    readOnlyControls = new List<Control>();
                }
                return readOnlyControls;
            }
        }

        /// <summary>
        /// 设置控件为只读状态
        /// </summary>
        public virtual void SetReadOnly()
        {
            SetReadOnly(true);
        }

        /// <summary>
        /// 设置控件为可编辑的状态
        /// </summary>
        public virtual void SetEditable()
        {
            SetReadOnly(false);
        }

        private void SetReadOnly(bool readOnly)
        {
            SetReadOnly(this, readOnly);
        }

        /// <summary>
        /// 【递归地】设置控件的只读属性
        /// </summary>
        /// <param name="parentCtrl"></param>
        /// <param name="readOnly"></param>
        private void SetReadOnly(Control parentCtrl, bool readOnly)
        {
            foreach (Control ctrl in parentCtrl.Controls)
            {
                if (ReadOnlyControls.Contains(ctrl))
                {
                    continue;
                }

                if (ctrl is TextBox)
                {
                    (ctrl as TextBox).ReadOnly = readOnly;
                }
                else if ((ctrl is Panel) || (ctrl is GroupBox))
                {
                    SetReadOnly(ctrl, readOnly);
                }
                else
                {
                    ctrl.Enabled = !readOnly;
                }
            }
        }
    }
}
